package com.xff.Controller;

import com.xff.bean.*;
import com.xff.dao.AdminDao;
import com.xff.dao.ApplyDao;
import com.xff.dao.BookDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class ApplyController {
    /*
     * 申请借阅
   * */
    @ResponseBody
    @GetMapping("/requestborrow")
    public String requestborrow(HttpServletRequest request){
        String username=request.getParameter("username");
        int bid=Integer.parseInt(request.getParameter("bid"));
        AdminDao adminDao=new AdminDao();
        AdminBean adminBean=adminDao.get_AidInfo3(username);

        if(new ApplyDao().get_ApplyListInfo(username,bid,0).size()>0){
            return "hasApply";
        }

        if(adminBean.getMax_num()>adminBean.getBorrowing_num()){
            BookDao bookdao = new BookDao();
            BookBean bookBean=bookdao.get_BookInfo(bid);

            ApplyBean applyBean=new ApplyBean(adminBean.getAid(),bookBean.getBid(),bookBean.getCard(),bookBean.getName(),adminBean.getUsername(),adminBean.getName());
            ApplyDao applyDao=new ApplyDao();
            applyDao.addApplyBook(applyBean);

            return "ok";
        }
        else
            return "overnum";
    }

    @ResponseBody
    @GetMapping("/getApplyList")
    public  ArrayList<ApplyBean> getApplyList(HttpServletRequest request,HttpSession session){
            String username = request.getParameter("username");
            ArrayList<ApplyBean> applyBeans=new ApplyDao().get_ApplyListInfo(username);

            return applyBeans;
        }

    @ResponseBody
    @GetMapping("/deleteApply")
    public String deleteApply(HttpServletRequest request,HttpSession session){
        int applyid =Integer.parseInt(request.getParameter("applyid"));
       new ApplyDao().deleteApply(applyid);
       return "ok";
    }



    @PostMapping("/SelectApply_DescServlet")
    public  String SelectLossServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int tip = Integer.parseInt(request.getParameter("tip"));
            String name = request.getParameter("name");
            ArrayList<ApplyBean> applyBeans=new ApplyDao().get_ApplyListInfo(tip,name,1);
            //将获取的结果存入请求中
            request.setAttribute("data", applyBeans);
            return "admin_apply";
        }
        else {
            return "redirect:/login";
        }
    }

    @PostMapping("/SelectApply_AsceServlet")
    public  String SelectApply_AsceServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int tip = Integer.parseInt(request.getParameter("tip"));
            String name = request.getParameter("name");
            ArrayList<ApplyBean> applyBeans=new ApplyDao().get_ApplyListInfo(tip,name,2);
            //将获取的结果存入请求中
            request.setAttribute("data", applyBeans);
            return "admin_apply";
        }
        else {
            return "redirect:/login";
        }
    }

    @PostMapping("/SelectApplyServlet")
    public  String SelectApplyServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int tip = Integer.parseInt(request.getParameter("tip"));
            String name = request.getParameter("name");
            ArrayList<ApplyBean> applyBeans=new ApplyDao().get_ApplyListInfo(tip,name,0);
            //将获取的结果存入请求中
            request.setAttribute("data", applyBeans);
            return "admin_apply";
        }
        else {
            return "redirect:/login";
        }
    }

    /*
    * 管理员同意借书
    * */
    @RequestMapping("/AgreeServlet")
    public  String AgreeServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int applyid =Integer.parseInt(request.getParameter("applyid"));
            int bid =Integer.parseInt(request.getParameter("bid"));
            int aid =Integer.parseInt(request.getParameter("aid"));

            /*插入一条借阅记录*/
            AdminBean admin = new AdminBean();
            AdminDao admindao = new AdminDao();
            BookDao bookdao = new BookDao();
            //通过aid获取到读者信息
            admin = admindao.get_AidInfo2(aid+"");
            //将借阅记录存入数据库
            bookdao.borrowBook(bid, admin);

            /*此书的可借阅数量-1*/
            BookBean bookBean=bookdao.get_BookInfo(bid);
            bookBean.setSubscribenum(bookBean.getSubscribenum()-1);
            bookdao.updateBook(bookBean);
            int num=bookBean.getSubscribenum();

            /*用户的当前借阅数量+1*/
            admin.setBorrowing_num(admin.getBorrowing_num()+1);
            admindao.updateUser(admin);

            /*修改apply表中的status属性*/
            new ApplyDao().updateApply_status(applyid,1);

            /*如果此书的可借阅数量为0，则有关此书的所有正在申请失效*/
            if(num==0){
                new ApplyDao().updateApply_allstatus(bookBean.getBid(),2);
            }
            /*如果读者的最大借阅数等于正在借阅数，则读者的所有正在申请失效*/
            if(admin.getBorrowing_num()==admin.getMax_num()){
                new ApplyDao().updateApply_userstatus(admin.getAid(),2);
            }

            return "admin_apply";
        }
        else
            return "redirect:/login";
    }

    /*
    * 管理员不同意借书
    * */
    @RequestMapping("/DisagreeServlet")
    public  String DisagreeServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int applyid =Integer.parseInt(request.getParameter("applyid"));
            new ApplyDao().updateApply_status(applyid,2);
            return "admin_apply";
        }
        else
            return "redirect:/login";
    }


    @ResponseBody
    @PostMapping("/SelectIsPenaltyServlet")
    public String SelectIsPenaltyServlet(HttpServletRequest request, HttpSession session){
        String name=request.getParameter("name");
        ArrayList<HistoryPenaltyBean> historyPenaltyBeans=new BookDao().get_HistoryPenaltyListInfo(name);
        if(historyPenaltyBeans.size()==0){
            return "no";
        }else
            return "yes";

    }

}
